package com.dhy.service.imp;

import com.dhy.mapper.PermissionMapper;
import com.dhy.pojo.Permission;
import com.dhy.service.IAutoScanPermissionService;
import com.dhy.utils.ClassUtil;
import com.dhy.utils.annotation.MyPermissionAnnotation;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Service
public class AutoScanPermissionServiceImpl implements IAutoScanPermissionService {

    @Value("${scan.tage}")
    private boolean tage;

    @Value("${scan.scanPackage}")
    private String scanPackage;

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public void scan() {

        List<Permission> permissionList = new ArrayList<>();

        //检查是否需要扫描权限配置
        if (this.tage) {
            //1.获取controller
            List<Class<?>> controllers = ClassUtil.fileDfs(this.scanPackage);
            System.out.println(controllers);
            //2.循环获取每一个Controller上的注解
            for (Class<?> controller : controllers) {
                MyPermissionAnnotation annotation = controller.getAnnotation(MyPermissionAnnotation.class);
                if (annotation != null) {
                    Permission permission = new Permission();
                    RequestMapping annotationRmp = controller.getAnnotation(RequestMapping.class);
                    if (annotationRmp != null) {
                        permission.setUrl(annotationRmp.value()[0]);
                    }
                    permission.setName(annotation.name());
                    permission.setDescs(annotation.desc());
                    permission.setSn(controller.getSimpleName());
                    if (permissionMapper.selectByName(permission.getName()) == null) {
                        //如果为空则加入权限集合进行批量插入
                        permissionList.add(permission);
                    } else {
                        //否则执行修改
                    }
                    //扫描controller的所有权限方法===============================================================================
                    for (Method method : controller.getMethods()) {
                        MyPermissionAnnotation annotation2 = method.getAnnotation(MyPermissionAnnotation.class);
                        if (annotation2 != null) {
                            Permission permission2 = new Permission();
                            String childUrl = getChildUrl(method);
                            permission2.setUrl(permission.getUrl()+childUrl);
                            permission2.setName(annotation2.name());
                            permission2.setDescs(annotation2.desc());
                            permission2.setSn(controller.getSimpleName()+":"+method.getName());
                            if (permissionMapper.selectByName(permission2.getName()) == null) {
                                //如果为空则加入权限集合进行批量插入
                                permissionList.add(permission2);
                            } else {
                                //否则执行修改
                            }
                        }
                    }
                }
            }
            if (permissionList != null) {
                System.out.println("======================================================"+permissionList);
                    permissionMapper.batchInsert(permissionList);
            }
        }
    }

    private String getChildUrl(Method method){

        RequestMapping annotation = method.getAnnotation(RequestMapping.class);
        if (annotation!=null){
            return annotation.value().length>0?annotation.value()[0]:"";
        }
        GetMapping getMapping = method.getAnnotation(GetMapping.class);
        if (getMapping!=null){
            return getMapping.value().length>0?getMapping.value()[0]:"";
        }
        PutMapping putMapping = method.getAnnotation(PutMapping.class);
        if (putMapping!=null){
            return putMapping.value().length>0?putMapping.value()[0]:"";
        }
        DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
        if (deleteMapping!=null){
            return deleteMapping.value().length>0?deleteMapping.value()[0]:"";
        }
        PostMapping postMapping = method.getAnnotation(PostMapping.class);
        if (postMapping!=null){
            return postMapping.value().length>0?postMapping.value()[0]:"";
        }
        PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
        if (patchMapping!=null){
            return patchMapping.value().length>0?patchMapping.value()[0]:"";
        }
        return "";
    }
}
